package com.bsoft.gol.hcs.specialist.dao;

/**
 * @Author:FangZhiyi
 * @Description
 * @Data Created in 2020/7/15 9:49
 * @Moddified By:
 */

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.converter.ConversionUtils;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import com.bsoft.gol.hcs.specialist.entity.FollowUpRouteDetail;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public abstract class FollowUpRouteDetailDAO extends HibernateSupportDelegateDAO<FollowUpRouteDetail> {

	@DAOMethod(sql = " from  FollowUpRouteDetail where organization_id=:orgId and route_id =:routeId order by follow_up_stage_first_seq desc")
	public abstract List<FollowUpRouteDetail> queryFollowUpRouteDetailList(@DAOParam("orgId") String organizationId,
																		   @DAOParam("routeId") Long routeId);

	@DAOMethod(sql = " from  FollowUpRouteDetail where route_id =:routeId ")
	public abstract List<FollowUpRouteDetail> queryByRouteId(@DAOParam("routeId") Long routeId);

	@DAOMethod(sql = " from  FollowUpRouteDetail where routeDetailId =:routeDetailId ")
	public abstract FollowUpRouteDetail getByRouteDetailId(@DAOParam("routeDetailId") Long routeDetailId);

	/**
	 * @param
	 * @throws
	 * @description: 先查询到所有未终止的随访生产记录对应的随访路径详情
	 * @return: java.util.List<specialist.entity.FollowUpRouteDetail>
	 * @author: tongtp
	 * @date: 2020/7/30 11:28
	 */
	public List<FollowUpRouteDetail> queryUnFinishedForProduceRecord() {
		HibernateStatelessResultAction<List<FollowUpRouteDetail>> action =
				new AbstractHibernateStatelessResultAction<List<FollowUpRouteDetail>>() {
					@Override
					public void execute(StatelessSession ss) throws Exception {
						StringBuilder sqlBuilder = new StringBuilder();
						sqlBuilder.append("SELECT " +
								"a.route_detail_id AS routeDetailId, " +
								"a.route_id AS routeId, " +
								"a.route_name AS routeName, " +
								"a.organization_id AS organizationId, " +
								"a.route_detail_type AS routeDetailType, " +
								"a.medical_record_id AS medicalRecordId, " +
								"a.medical_record_name AS medicalRecordName, " +
								"a.time_node_type AS timeNodeType, " +
								"a.time_node_size AS timeNodeSize, " +
								"a.time_node_size_unit AS timeNodeSizeUnit, " +
								"a.follow_up_frequency AS followUpFrequency, " +
								"a.follow_up_frequency_time AS followUpFrequencyTime, " +
								"a.follow_up_frequency_number AS followUpFrequencyNumber, " +
								"a.is_notify AS isNotify, " +
								"a.notify_date AS notifyDate, " +
								"a.gmt_create AS gmtCreate, " +
								"a.gmt_modified AS gmtModified " +
								"FROM " +
								"follow_up_route_detail AS a " +
								"INNER JOIN (select DISTINCT b.route_detail_id from follow_up_plan_produce_record b " +
								"where b.status = 1) AS c ON a.route_detail_id = c.route_detail_id ");
						Query query = ss.createSQLQuery(sqlBuilder.toString());
						query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
						List<FollowUpRouteDetail> followUpRouteDetails = new ArrayList<>();
						List<Map> list = query.list();
						if (list.size() > 0) {
							for (Map map : list) {
								followUpRouteDetails.add(ConversionUtils.convert(map, FollowUpRouteDetail.class));
							}
							setResult(followUpRouteDetails);
						} else {
							setResult(null);
						}
					}
				};
		HibernateSessionTemplate.instance().execute(action);
		return action.getResult();
	}
}
